//
//  ChangeMobileViewController.m
//  tingche
//
//  Created by 尤振宇 on 2017/6/19.
//  Copyright © 2017年 尤振宇. All rights reserved.
//

#import "ChangeMobileViewController.h"
#import "ModifyMobileApi.h"
#import "VerificationCodeApi.h"
#import "WKCountdownButton.h"

@interface ChangeMobileViewController ()
@property (weak, nonatomic) IBOutlet UITextField *mobileTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet WKCountdownButton *getCode;
@end

@implementation ChangeMobileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.getCode roundedCorners_5PX];
    self.view.backgroundColor = [UIColor whiteColor];
//    if ([Config isLogin]) {
//        NSString *mobile = [Config userDefaultsGetObjectForKey:kMobile];
//        self.currentMobile.text = [NSString stringWithFormat:@"当前手机号: %@",mobile];
//    }
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)getCodeAction:(id)sender
{
    [self.mobileTF resignFirstResponder];
    if (![self.mobileTF.text validateMobile]) {
        [MBProgressHUD showError:@"请输入正确手机号"];
        return;
    }
    
    [self.getCode start];
    
    VerificationCodeApi *api = [[VerificationCodeApi alloc]initWithMoblle:self.mobileTF.text];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
        if (kCode(request.responseJSONObject)) {
            [MBProgressHUD showSuccess:[request.responseJSONObject objectForKey:@"message"]];
        }else{
            [self.getCode stop];
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self.getCode stop];
    }];
}

- (IBAction)OKAction:(id)sender {
    if (![self.mobileTF.text validateMobile]) {
        [MBProgressHUD showError:@"请输入正确手机号"];
        return;
    }
    if (![self.codeTF hasText]) {
        [MBProgressHUD showError:@"请输入验证码"];
        return;
    }
    
    
    WeakSelf(self)
    ModifyMobileApi *api = [[ModifyMobileApi alloc]initWithMobile:self.mobileTF.text verify:self.codeTF.text];
    api.needHUD = YES;
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
        if (kCode(request.responseJSONObject)) {
            [Config userDefaultsSetObject:weakself.mobileTF.text key:kMobile];
            [[NSNotificationCenter defaultCenter]postNotificationName:kMobileChangeNotification object:nil];
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
